package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 上传头像
     *
     * @param file
     * @param token
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity<Object> saveLogo(@RequestParam("headPhoto") MultipartFile file, @RequestHeader("Authorization") String token) {
        try {
            boolean flag = userInfoService.saveUserLogo(file, token);
            if (flag)
                return ResponseEntity.ok(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("00004").errMessage("用户信息头像上传失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
